// 基础地址 设置成常数
const BASE_URL = 'http://110.41.43.118:3008';

// 页数 页商品数
let page = 1;
let pagesize = 10;

// 获取元素
let oGoods = document.querySelector('.goods-content ul');
let oSeckillGoods = document.querySelector('.jd-seckill-goods ul');
let oNavList = document.querySelector('.nav-list ul');
// console.log(oGoods);
// console.log(oSeckillGoods);
// console.log(oNavList);


// 默认调用一次
getGoods();

carousel();

// 封装请求获取数据
function getGoods(){
	lb.ajax({
		url: BASE_URL + '/api_goods',
		data: {page,pagesize},
		success(res){
			console.log(res);
			if(res.code != 0){
				console.log(res);
				return;
			}
			
			// Dom渲染
			let str2 = '';
			res.data.forEach(item => {
				str2 += `
					<li>
						<div class="seckill-goods-msg">
							<img src="${item.goods_thumb}" alt="" />
							<span>￥${item.price}</span>
						</div>
					</li>
				`;
			});
			oSeckillGoods.innerHTML = str2;
			
			let str3 = '';
			res.data.forEach(item => {
				str3 += `
					<li>
						<div class="goods-img">
							<img src="${item.goods_thumb}" alt="" />
						</div>
						<div class="goods-msg">
							<p>${item.goods_name}</p>
							<div>￥<span>${item.price}</span>.90</div>
							<div class="goods-msg-down">
								<div><img src="img/JD_goods_ziy.png" alt="" /><span>20万+评论</span></div>
								<div>看相似</div>
							</div>
						</div>
					</li>
				`;
			});
			oGoods.innerHTML = str3;
			
		}
	})
};

// 封装轮播
function carousel(){
	
	let num1 = 0;
	let num2 = 0;
	let num3 = 0;
	let num4 = 0;
	let num5 = 0;
	let num6 = 0;
	// 触摸事件
	
	oNavList.addEventListener('touchstart', function(event){
		num1 = event.touches[0].clientX;
	})
	oNavList.addEventListener('touchmove', function(event){
		num2 = event.touches[0].clientX;
		num3 = num1 - num2;
		// console.log(num);
		if(num3 > 0){
			oNavList.style.left = -350 + 'px';
		}
		if(num3 < 0){
			oNavList.style.left = 0 + 'px';
		}
	})
	
	oSeckillGoods.addEventListener('touchstart', function(event){
		num4 = event.touches[0].clientX;
	})
	oSeckillGoods.addEventListener('touchmove', function(event){
		num5 = event.touches[0].clientX;
		num = num4 - num5;
		// console.log(num);
		if(num6 > 0){
			oSeckillGoods.style.left = -num + 'px';
		}
		if(num6 < 0){
			oSeckillGoods.style.left = num + 'px';
		}
	})
}





